package com.xiangying.config;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis配置
 * @author luolihong
 *
 */
@Configuration
@EnableCaching
public class RedisConfigurer extends CachingConfigurerSupport {
	
	private Logger logger = LoggerFactory.getLogger(RedisConfigurer.class);
	
	/** IP地址 */
    @Value("${redis.host}")
    private String host;
    
    /** 端口 */
    @Value("${redis.port}")
    private int port;
    
    /** 授权验证 */
    @Value("${redis.auth}")
    private String auth;

    /** 超时时间 */
    @Value("${redis.timeout}")
    private int timeout;

    /** 最大空闲数 */
    @Value("${redis.pool.max-idle}")
    private int maxIdle;

    /** 最大等待时间 */
    @Value("${redis.pool.max-wait}")
    private long maxWaitMillis;

    @Bean(name = "jedisPool")
    public JedisPool redisPoolFactory() {
    	logger.info("redis地址：" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = null;
        if(StringUtils.isEmpty(auth)){
        	jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
		} else {
			jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, auth);
		}
        logger.info("JedisPool注入成功");
        return jedisPool;
    }

}